using Unity.VisualScripting;
using UnityEngine;

public class SingtonMono<T> : MonoBehaviour where T : SingtonMono<T>
{
    //继承Mono的单例基类
    private static T instance;

    public static T Instance {
        get
        {
            if (instance == null)
            {
                instance = FindFirstObjectByType<T>();
                if (instance == null)
                {
                    GameObject obj = new GameObject("Manager");
                    obj.AddComponent<T>();
                    return instance;
                }
              
            }
            return instance;
        }
    }
            


    protected virtual void Awake()
    {
        if (instance == null) { 
            instance = this as T;
        }
    }

    private void OnDestroy()
    {
        if (instance == this) {
            instance = null;
        }
    }
}
